package com.web.my12306.dao.impl;

import com.web.my12306.dao.ISeatDao;
import com.web.my12306.pojo.Seat;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class SeatDaoImpl extends BaseDao implements ISeatDao{
    @Override
    /**
     * 黄逸9.13
     * */
    public List<Seat> get(int tid,int state) {
        String sql = "select sid,tid,carriage,seatno,state from seat where tid=? and state=?";
        query(sql,tid,state);
        List<Seat> seats = new ArrayList<>();
        try {
            while (rs.next()) {
                Seat seat = new Seat();
                seat.setSid(rs.getInt("sid"));
                seat.setTid(rs.getInt("tid"));
                seat.setCarriage(rs.getInt("carriage"));
                seat.setSeatno(rs.getString("seatno"));
                seat.setState(rs.getInt("state"));
                seats.add(seat);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
            return seats;
    }

    @Override
    public void add(Seat seat) {
        try
        {
            update("insert into seat(tid,carriage,seatno,state) values(?,?,?,?)", seat.getTid(), seat.getCarriage(), seat
                    .getSeatno(), seat.getState());
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public boolean editState(int sid,int state)
    {
        update("update seat set state=? where sid=?",state,sid);
        return true;
    }
}
